package com.techsen.tsweb.sys.dao.interceptor;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

import com.techsen.tsweb.core.domain.BaseEntity;
import com.techsen.tsweb.core.util.SubjectUtil;
import com.techsen.tsweb.core.util.UUIDUtil;
import com.techsen.tsweb.core.util.ValidUtil;

@Component("localMybatisInterceptor")
@Intercepts(@Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }))
@SuppressWarnings({ "rawtypes", "unchecked" })
public class LocalMybatisInterceptor implements Interceptor {

    private Properties properties;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
        Object parameter = invocation.getArgs()[1];
        String id = statement.getId();
        String idSuffix = id.substring(id.lastIndexOf(".") + 1);
        
        /**
         * 添加时，判断是否有ID，如果没有ID，随机生成一个ID<br/>
         * 设置添加此数据的用户及添加的日期时间
         */
        if ("add".equals(idSuffix)) {
            if (parameter instanceof BaseEntity) {
                BaseEntity entity = (BaseEntity) parameter;
                if (!ValidUtil.isValid(entity.getId())) {
                    entity.setId(UUIDUtil.uuid());
                }
                try {
                    entity.setCreateBy(SubjectUtil.getPrincipal());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                entity.setCreateDate(new Date());
            }
        }
        /**
         * 批量添加
         */
        else if ("batchAdd".equals(idSuffix)) {
            if (parameter instanceof Map) {
                Map map = (Map) parameter;
                List<BaseEntity> entities = (List<BaseEntity>) (map.get("list"));
                if (ValidUtil.isValid(entities)) {
                    for (BaseEntity entity : entities) {
                        if (!ValidUtil.isValid(entity.getId())) {
                            entity.setId(UUIDUtil.uuid());
                        }
                        try {
                            entity.setCreateBy(SubjectUtil.getPrincipal());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        entity.setCreateDate(new Date());
                    }
                }
            }
        }
        /**
         * 修改时，<br/>
         * 设置修改此数据的用户及修改的日期时间
         */
        else if ("update".equals(idSuffix)) {
            if (parameter instanceof BaseEntity) {
                BaseEntity entity = (BaseEntity) parameter;
                try {
                    entity.setUpdateBy(SubjectUtil.getPrincipal());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                entity.setUpdateDate(new Date());
            }
        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public Properties getProperties() {
        return properties;
    }

}
